﻿using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media;
using MTBrainStorm.Framework.Messaging;

namespace MTBrainStorm.Multitouch.Behaviors
{
    public class ManipulationContainerBehavior : Behavior<UIElement>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            
            AssociatedObject.ManipulationStarting += AssociatedObjectManipulationStarting;
            AssociatedObject.ManipulationDelta += AssociatedObjectManipulationDelta;
            AssociatedObject.ManipulationInertiaStarting += AssociatedObjectManipulationInertiaStarting;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();

            AssociatedObject.ManipulationStarting -= AssociatedObjectManipulationStarting;
            AssociatedObject.ManipulationDelta -= AssociatedObjectManipulationDelta;
            AssociatedObject.ManipulationInertiaStarting -= AssociatedObjectManipulationInertiaStarting;
        }

        #region Event Handlers

        void AssociatedObjectManipulationStarting(object sender, ManipulationStartingEventArgs e)
        {
            e.ManipulationContainer = AssociatedObject;
            e.Handled = true;
        }

        void AssociatedObjectManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            var element = e.Source as FrameworkElement;
            if (element != null)
            {
                var transform = element.RenderTransform;

                var deltaManipulation = e.DeltaManipulation;
                var matrix = transform.Value;

                var center = e.ManipulationOrigin;
                center = AssociatedObject.TranslatePoint(center, element);

                center = matrix.Transform(center);
                
                matrix.Translate(e.DeltaManipulation.Translation.X, e.DeltaManipulation.Translation.Y);

                matrix.RotateAt(e.DeltaManipulation.Rotation, center.X, center.Y);

                matrix.ScaleAt(deltaManipulation.Scale.X, deltaManipulation.Scale.Y, center.X, center.Y);

                element.RenderTransform = new MatrixTransform(matrix);
                
                var args = new ManipulationArgs
                            {
                                Source = element.DataContext,
                                Position = new Point(matrix.OffsetX, matrix.OffsetY),
                                Rotation = e.DeltaManipulation.Rotation,
                                ScaleX = e.DeltaManipulation.Scale.X,
                                ScaleY = e.DeltaManipulation.Scale.Y,
                                ManipulationOrigin = e.ManipulationOrigin
                            };

                Messenger.Default.Send(args, element.DataContext);
                e.Handled = true;

                //TODO pozwolic obiektowi wyjechac na ok 3/4 wielkosci
                if (e.IsInertial)
                {
                    var containingRect = new Rect(((FrameworkElement)e.ManipulationContainer).RenderSize);
                    
                    var shapeBounds = element.RenderTransform.TransformBounds(new Rect(element.RenderSize));

                    if (e.IsInertial && !containingRect.Contains(shapeBounds))
                    {
                        e.ReportBoundaryFeedback(e.DeltaManipulation);
                        e.Complete();
                    }
                }
            }
        }

        static void AssociatedObjectManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e)
        {
            e.TranslationBehavior = new InertiaTranslationBehavior()
            {
                InitialVelocity = e.InitialVelocities.LinearVelocity,
                DesiredDeceleration = 10.0 * 96.0 / (1000.0 * 1000.0)
            };

            e.ExpansionBehavior = new InertiaExpansionBehavior()
            {
                InitialVelocity = e.InitialVelocities.ExpansionVelocity,
                DesiredDeceleration = 0.1 * 96 / 1000.0 * 1000.0
            };

            e.RotationBehavior = new InertiaRotationBehavior()
            {
                InitialVelocity = e.InitialVelocities.AngularVelocity,
                DesiredDeceleration = 720 / (1000.0 * 1000.0)
            };
            e.Handled = true;
        }

        #endregion

    }
}
